<template>
  <div class="xcglfxManager">
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.jsaanalysis_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot="xzaqfxForm">
        <div
          align="center">
          <el-button type="primary"
                     size="small"
                     icon="el-icon-plus"
                     @click="openxzaqfx">新增安全分析
          </el-button>
        </div>
      </template>
      <template slot="xzaqfxbgForm">
        <div class="fxgkcsContent_div" v-if="aqfxData.length>0">
          <avue-crud
            :option="aqfxoption"
            :data="aqfxData"
            @row-update="aqfxrowUpdate"
            @row-del="aqfxrowDel"
            ref="cmCrud"
          >
          </avue-crud>
        </div>
      </template>
    </avue-crud>
      <jsaanalysisitem
        :aqfxBox="aqfxBox"
        @chooseControlMeasure="chooseControlMeasure"
        @cancelChooseControlMeasure="cancelChooseControlMeasure"
        @handleCloseChooseMeasure="handleCloseChooseMeasure"
      >
      </jsaanalysisitem>

  </basic-container>
  </div>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/xcgl/jsaanalysis";
  import {mapGetters} from "vuex";
  import jsaanalysisitem from '@/views/xcgl/jsaanalysisitem'

  export default {
    components:{
      jsaanalysisitem
    },
    data() {
      return {
        dialogType: "",
        viewLabel: "view",
        aqfxoption: {
          addBtn:false,
          calcHeight: 30,
          tip: false,
          // editBtn:false,
          // delBtn:false,
          searchMenuSpan: 6,
          refreshBtn:false,
          header:false,
          dialogClickModal: false,
          column: [
            {
              label: "工作步骤",
              prop: "gzbz",
              align:'center',
              type:"select",
              dataType:'number',
              width:100,
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=xcgl_gzbz",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入工作步骤",
                trigger: "blur"
              }]
            },
            {
              label: "作业类型",
              prop: "zylx",
              align:'center',
              type:"select",
              dataType:'number',
              width:100,
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=xcgl_zylx",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入作业类型",
                trigger: "blur"
              }]
            },
            {
              label: "危险因素",
              prop: "wxys",
              align:'center',
              type: "textarea",
              maxlength:100,
              span:24,
              rules: [{
                required: true,
                message: "请输入危险因素",
                trigger: "blur"
              }]
            },
            {
              label: "导致的后果",
              prop: "dzdhg",
              align:'center',
              type: "textarea",
              maxlength:100,
              span:24,
              rules: [{
                required: true,
                message: "请输入导致的后果",
                trigger: "blur"
              }]
            },
            {
              label: "控制措施",
              prop: "kzcs",
              align:'center',
              type: "textarea",
              maxlength:100,
              span:24,
              rules: [{
                required: true,
                message: "请输入控制措施",
                trigger: "blur"
              }]
            },
          ]
        },


        aqfxData:[],
        aqfxBox:false,
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "编号",
              prop: "jsaNo",
              align: "center",
              search:true,
              readonly:true,
              rules: [{
                required: true,
                message: "请输入编号",
                trigger: "blur"
              }]
            },
            {
              label: "申请日期",
              prop: "sqDate",
              align: "center",
              search:true,
              type:"date",
              format: "yyyy-MM-dd",
              valueFormat: "yyyy-MM-dd",
              rules: [{
                required: true,
                message: "请输入申请日期",
                trigger: "blur"
              }]
            },
            {
              label: "主持人",
              prop: "zcr",
              align: "center",
              rules: [{
                required: true,
                message: "请输入主持人",
                trigger: "blur"
              }]
            },
            {
              label: "作业单位",
              prop: "zydw",
              align: "center",
              rules: [{
                required: true,
                message: "请输入作业单位",
                trigger: "blur"
              }]
            },
            {
              label: "作业地点",
              prop: "zydd",
              align: "center",
              rules: [{
                required: true,
                message: "请输入作业地点",
                trigger: "blur"
              }]
            },
            {
              label: "工作内容",
              prop: "gznr",
              align: "center",
              type: "textarea",
              span:24,
              rules: [{
                required: true,
                message: "请输入工作内容",
                trigger: "blur"
              }]
            },
            {
              label: "参与人",
              prop: "cyr",
              align: "center",
              span:24,
              rules: [{
                required: true,
                message: "请输入参与人",
                trigger: "blur"
              }]
            },
            {
              label: "",
              prop: "xzaqfx",
              align: "center",
              span:24,
              hide:true,
              formslot:true,
            },
            {
              label: "",
              prop: "xzaqfxbg",
              align: "center",
              span:24,
              hide:true,
              formslot:true,
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.jsaanalysis_add, false),
          viewBtn: this.vaildData(this.permission.jsaanalysis_view, false),
          delBtn: this.vaildData(this.permission.jsaanalysis_delete, false),
          editBtn: this.vaildData(this.permission.jsaanalysis_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      aqfxrowUpdate(row, index, done) {
        this.aqfxData[index].gzbz=row.gzbz
        this.aqfxData[index].zylx=row.zylx
        this.aqfxData[index].wxys=row.wxys
        this.aqfxData[index].dzdhg=row.dzdhg
        this.aqfxData[index].kzcs=row.kzcs
        done()
      },
      aqfxrowDel(row,index) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            this.aqfxData.splice(index, 1);
          })

      },
      // 选择
      chooseControlMeasure(list) {
        list.forEach(item => {
          this.aqfxData.push({
            gzbz: item.gzbz,
            zylx: item.zylx,
            wxys: item.wxys,
            dzdhg: item.dzdhg,
            kzcs: item.kzcs,
          });
        });
        this.cancelChooseControlMeasure();
      },

      // 取消选择管控措施
      cancelChooseControlMeasure() {
        this.aqfxBox = false;
      },

      // 关闭选择管控措施弹窗
      handleCloseChooseMeasure() {
        this.cancelChooseControlMeasure();
      },
      openxzaqfx(){
        this.aqfxBox=true
      },
      rowSave(row, done, loading) {
        row.jsaAnalysisItems=this.aqfxData
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        this.aqfxData=[]
        this.dialogType = type;

        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
            this.aqfxData=this.form.jsaAnalysisItems
          });
        }else{
          const d =new Date();
          this.form={
            jsaNo:'JSA-'+d.getFullYear()+d.getMonth()+d.getDay()+d.getHours()+d.getMinutes()+d.getSeconds()+d.getMilliseconds()
          }
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style lang="scss" scoped>
  .choose_cm_footer {
    text-align: center;
    margin-top: 15px;

    .el-button {
      width: 100px;
      height: 35px;
    }
  }
  .fxgkcsContent_div {
    .avue-crud {
      .el-table__empty-block,
      .el-table__header-wrapper,
      .avue-crud__menu,
      .avue-crud__pagination {
        display: none;
      }
    }
  }
</style>
